# Tem Twitter Module.
# MODULE VERSION: 0.1 (BETA)
# It should be highly noted that I tried to use the twitter gem and its oauth settings to create this module but it has a problem with HMAC and Ruby 1.9 
# and until it is resolved I will use a basic HTTP REST Method. 

# Functions:
# .twitter status update <update text> - Update your twitter status.
# .twitter get last - Prints out your current last status update.
# .twitter follow <screen name> - Follow a twitter screen name.

# Change these options.
$Twit_User = 'Jeedey'
$Twit_Password = '**********'

require 'net/http'
require 'base64'
class Twit_Module
	def initialize(command_handler, trigger, subject, parameters, authed)
		@subject = subject
		@command_handler = command_handler
		@trigger = trigger
		@parameters = parameters
		@http_handle = Net::HTTP.new("twitter.com", 80)
		@http_headers = {"Content-Type" => "text/xml", "Authorization" => "Basic "+Base64.encode64($Twit_User+":"+$Twit_Password)}
		if @parameters =~ /^status update/
			# Seems they want to update status.
			@parameters = @parameters.gsub /^status update/, '' # Clip to leave just the status text
			status_update(@parameters)
		elsif @parameters =~ /^get last/
			@parameters = @parameters.gsub /^get last/, '' 
			get_last_status()
		elsif @parameters =~ /^follow/
			@parameters = @parameters.gsub /^follow/, ''
			@parameters.slice!(0)
			follow_name(@parameters)
		end
	end	

	def status_update(msg)
		if(msg.length > 139)
			@command_handler.privmsg(@subject, "[Twitter]  Your status update exceeds 140 characters and will be truncated by twitter.")
		end
		xml_req_path = "/statuses/update.xml"
		xml_req = "<status>#{msg}</status>"
		response_code, data = @http_handle.post(xml_req_path, xml_req, @http_headers)
		if response_code =~ /403/ or data =~ /\<error\>/
			@command_handler.privmsg(@subject, "[Twitter] An error occured, it seems to be down to going over the request limit or authentication problems.")
		else
			@command_handler.privmsg(@subject, "[Twitter] Status Successfully Updated.")
		end
	end

	def get_last_status()
		xml_req_path = "/statuses/user_timeline.xml?screen_name="+$Twit_User+"&count=1"
		response_code, data = @http_handle.get(xml_req_path, @http_headers)
		if response_code =~ /403/ or data =~ /\<error\>/
			@command_handler.privmsg(@subject, "[Twitter] An error occured, it seems to be down to going over the request limit or authentication problems.")
		else
			status = data.match(/\<text\>(.+)\<\/text\>/)
			created_at = data.match(/\<created_at\>(.+)\<\/created_at\>/)
			if status and created_at
				@command_handler.privmsg(@subject, "[Twitter] Last user status:")
				@command_handler.privmsg(@subject, "[Twitter] Created on: "+ created_at.captures[0])
				@command_handler.privmsg(@subject, "[Twitter] Status: "+ status.captures[0])
			else
				@command_handler.privmsg(@subject, "[Twitter] Unknown error :(")
			end
		end
	end

	def follow_name(screen_name)
		xml_req_path = "/friendships/create/id.xml?screen_name="+screen_name
		xml_req = "<screen_name>#{screen_name}</screen_name>"
		response_code, data = @http_handle.post(xml_req_path, xml_req, @http_headers)
		if response_code =~ /403/ or data =~ /\<error\>/
			privmsg(@subject, "[Twitter] An error occured, it seems to be down to going over the request limit or authentication problems.")
		else
			status = data.match(/\<screen_name\>#{screen_name}\<\/screen_name\>/)
			if status
				@command_handler.privmsg(@subject, "[Twitter] You are now following "+screen_name+" successfully!")
			else
				@command_handler.privmsg(@subject, "[Twitter] Unknown error :(")
			end
		end
	end
end

$Loaded_Modules["twitter"] = "Twit_Module"
